﻿using MSAcademic.v2.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSAcademic.v2.Infrastructure.SubSection.Impl
{
    public class SubSectionCommandImpl : ISubSectionCommand
    {
        public void Create(string name, int contentsectionId , string thumbnailUrl)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbSubSection = new Database.SubSection
                {
                    Name = name,
                    ContentSectionId=contentsectionId,
                    IsHidden = false,
                    ThumbnailUrl = thumbnailUrl
                    

                };


                db.SubSections.Add(dbSubSection);
                db.SaveChanges();
            }
        }

        public void Edit(int id, string name, string thumbnailUrl)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbSubSection = db.SubSections.SingleOrDefault(x => x.Id == id && x.IsHidden == false);

                if (dbSubSection == null)
                {
                    return;
                }

                if (name != null)
                {
                    dbSubSection.Name = name;
                }
                else
                {
                    dbSubSection.ThumbnailUrl = thumbnailUrl;
                }

                db.SaveChanges();
            }
        }

        public int Delete(int id)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbSubSection = db.SubSections.SingleOrDefault(x => x.Id == id && x.IsHidden == false);

                if (dbSubSection == null)
                {
                    return 0;
                }

                dbSubSection.IsHidden = true;
                db.SaveChanges();
                return dbSubSection.ContentSectionId;
            }

        }
    }
}
